package com.seio.edc.controller.description;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.nova.edc.model.system.Description;
import com.nova.edc.model.system.Login;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.energy.GatherSummaryService;
import com.nova.edc.service.energy.OverallStatisticsService;
import com.nova.edc.service.system.DescriptionService;
import com.nova.edc.service.system.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.nova.edc.model.system.CustomerInfo;
import com.nova.edc.service.energy.BillStatisticsService;
import com.nova.edc.service.system.CustomerInfoService;
import com.seio.edc.util.DateTimeUtil;

@Controller
public class DescriptionContoller {

    @Autowired
    private CustomerInfoService novaCustomerInfoService;

    @Autowired
    private GatherSummaryService novaGatherSummaryService;

    @Autowired
    private com.nova.edc.service.system.LoginService novaLoginService;

    @Autowired
    private DescriptionService novaDescriptionService;

    @Autowired
    private RoleService novaRoleService;


    /**
     * 项目介绍
     */
    @RequestMapping(value = "descriptionInit")
    public ModelAndView index(HttpServletRequest request, ModelMap map) {
        Login login = (Login) request.getSession().getAttribute("sessionLogin");
        String loginname = (String) request.getSession().getAttribute(
                "loginname");

        // 获取相应的节省电费等信息
        // 获取登录用户相应的节省的电费和价格
        List<CustomerInfo> customerInfos = null;
        if (login.getRoletype() == ModelConstant.ROLE_GROUP) {
            customerInfos = novaCustomerInfoService.findByGroupcode(login.getGroupcode());
        } else {
            customerInfos = novaCustomerInfoService
                    .findCustomerInfoListByRole(loginname, null, "", "");
        }
        List<Integer> customerInfoIds = new ArrayList<Integer>();
        for (CustomerInfo info : customerInfos) {
            customerInfoIds.add(info.getId());
        }
//        Map<String, BigDecimal> statistics = novaGatherSummaryService
//                .getAllStatistics(customerInfoIds, ModelConstant.MIN_DATE, ModelConstant.MAX_DATE);
//        map.put("statistics", statistics);
//        Map<String, BigDecimal> unitStatistics = novaGatherSummaryService
//                .getUnitStatistics(
//                        customerInfoIds,
//                        DateTimeUtil.getNowTime(DateTimeUtil.YYYY_MM_DD),
//                        DateTimeUtil.addDays(-7, DateTimeUtil.YYYY_MM_DD));
//
//        map.put("unitStatistics", unitStatistics);
        Integer systemId = novaRoleService.findByType(ModelConstant.ROLE_SYSTEM).get(0).getId();
        Integer customerinfoid = novaLoginService.findByRoleId(systemId).get(0).getCustomerinfoid();
        Description description = novaDescriptionService.getByLogin(login, customerinfoid);
        map.put("description", description);
        return new ModelAndView("description/companyDescription");
    }
}
